﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoFlatnessTest
{
    /// <summary>
    /// UcBoard.xaml 的交互逻辑
    /// </summary>
    public partial class UcBoard : UserControl
    {
        public List<UcBoardData> Datas { get; } = new List<UcBoardData>();

        public UcBoard()
        {
            InitializeComponent();

            if (!WpfHelp.IsInDesignMode(this))
            {
                DatasClear();
            }
        }

        public void DatasSet(int row, int Column)
        {
            this.Dispatcher.Invoke(() =>
            {
                //清空表格
                Datas.Clear();
                tb.Children.Clear();
                tb.RowDefinitions.Clear();
                tb.ColumnDefinitions.Clear();

                //添加行列
                for (int i = 0; i < row; i++)
                {
                    tb.RowDefinitions.Add(new RowDefinition());
                }
                for (int i = 0; i < Column; i++)
                {
                    tb.ColumnDefinitions.Add(new ColumnDefinition());
                }

                int n = 1;
                //添加数据
                for (int r = 0; r < row; r++)
                {
                    for (int c = 0; c < Column; c++)
                    {
                        UcBoardMin ucBoardMin = new UcBoardMin();
                        ucBoardMin.VModel.TxtTiT = $"{n}({r + 1},{c + 1})";
                        tb.Children.Add(ucBoardMin);

                        //Grid.SetRow(ucBoardMin, r);
                        //Grid.SetColumn(ucBoardMin, c);

                        //反转矩阵
                        Grid.SetRow(ucBoardMin, row - r - 1);
                        Grid.SetColumn(ucBoardMin, Column - c - 1);

                        Datas.Add(new UcBoardData()
                        {
                            Row = r + 1,
                            Column = c + 1,
                            Num = n,
                            Data = ucBoardMin,
                        });

                        n++;
                    }
                }
            });
        }


        public void DatasClear()
        {
            this.Dispatcher.Invoke(() =>
            {
                //清空表格
                tb.Children.Clear();
                tb.RowDefinitions.Clear();
                tb.ColumnDefinitions.Clear();
            });
        }

        public void StopAll()
        {
            try
            {
                foreach (var item in Datas)
                {
                    item.Data.VModel.Stop();
                }
            }
            catch (Exception)
            {

                throw;
            }

        }

    }

    public class UcBoardData
    {
        public int Row { get; set; }
        public int Column { get; set; }
        public int Num { get; set; }
        public UcBoardMin Data { get; set; }
    }

}
